package org.jeecg.modules.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.jeecg.modules.entity.*;
import org.jeecg.modules.service.DiyProfessiorPointService;
import org.jeecg.modules.service.DiyProfessorScoreService;
import org.jeecg.modules.service.IDiyProjectProfessorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class TimedTask {
    @Autowired
    private DiyProfessorScoreService diyProfessorScoreService;
    @Autowired
    private DiyProfessiorPointService diyProfessiorPointService;
    @Autowired
    private IDiyProjectProfessorService iDiyProjectProfessorService;

    @Scheduled(cron = "*/10 * * * * ?")
    public void oneClockDay()throws Exception{
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy ");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(formatter.format(date));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        List<DiyProfessorScore> diyProfessorScores2 = diyProfessorScoreService.getBaseMapper().selectList(null);
        //获取下一年
        Serializable serializable1 = new Serializable();
        Serializable serializable2 = new Serializable();
        calendar.add(Calendar.YEAR,0);
        serializable1.setBeginDate(String.valueOf(calendar.get(Calendar.YEAR)));
        calendar.add(Calendar.YEAR, 1);
        serializable1.setOverDate(String.valueOf(calendar.get(Calendar.YEAR)));
        List<ProjectPictureInfo> list=diyProfessorScoreService.getTotal(serializable1);
        list.forEach(System.out::println);
        list.forEach(ProjectPictureInfo->{
                    UpdateWrapper<DiyProfessorScore> wrapper = new UpdateWrapper<>();
                    //判断状态类型 1==正常完成项目评标
                    if (ProjectPictureInfo.getX1()==1){
                        wrapper.eq("ding_user_id",ProjectPictureInfo.getZ()).
                                set("evaluation_number",ProjectPictureInfo.getY());
                        diyProfessorScoreService.getBaseMapper().update(null,wrapper);
                    }else if (ProjectPictureInfo.getX1()==0){
                        wrapper.eq("ding_user_id",ProjectPictureInfo.getZ()).
                                set("reject_item_score",ProjectPictureInfo.getY());
                        diyProfessorScoreService.getBaseMapper().update(null,wrapper);
                    }
                    else if (ProjectPictureInfo.getX1()==-4){
                        wrapper.eq("ding_user_id",ProjectPictureInfo.getZ()).
                                set("rejection_score",ProjectPictureInfo.getY());
                        diyProfessorScoreService.getBaseMapper().update(null,wrapper);
                    }

                }
        );
        for (int i = 0; i < diyProfessorScores2.size(); i++) {
            int a= 0;
            int b = 0;
            int count = 1;
            QueryWrapper<DiyProjectProfessor> queryWrapper = new QueryWrapper<>();
            queryWrapper.apply("date_format( diy_project_professor.create_time, '%Y') between '2021' and '2022'")
                    .eq("ding_user_id",diyProfessorScores2.get(i).getDingUserId());
            List<DiyProjectProfessor> diyProjectProfessors = iDiyProjectProfessorService.list(queryWrapper);
            for (int i1 = 0; i1 < diyProjectProfessors.size(); i1++) {
                a= diyProjectProfessors.get(i1).getProfessorInProjectProfessorStar()+a;
                b=diyProjectProfessors.get(i1).getUgentScore()+b;
                System.out.println(diyProfessorScores2.get(i).getDingUserId());
            }
            UpdateWrapper<DiyProfessorScore> wrapper2 = new UpdateWrapper<>();
            wrapper2.eq("ding_user_id",diyProfessorScores2.get(i).getDingUserId()).set("item_rating",a/count).set("ugent_score",b);
            count++;
            diyProfessorScoreService.update(null,wrapper2);
        }
        //获取上一年
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(new Date());
        calendar2.add(Calendar.YEAR,-1);
        serializable2.setBeginDate(String.valueOf(calendar2.get(Calendar.YEAR)));
        calendar2.add(Calendar.YEAR, 0);
        serializable2.setOverDate(String.valueOf(calendar2.get(Calendar.YEAR)));
        List<ProjectPictureInfo> list2=diyProfessorScoreService.getTotal(serializable2);
        list2.forEach(ProjectPictureInfo->{
                    UpdateWrapper<DiyProfessorScore> wrapper = new UpdateWrapper<>();
                    //判断状态类型 1==正常完成项目评标
                    if (ProjectPictureInfo.getX1()==1){
                        wrapper.eq("ding_user_id",ProjectPictureInfo.getZ()).
                                set("evaluation_number2",ProjectPictureInfo.getY());
                        diyProfessorScoreService.getBaseMapper().update(null,wrapper);
                    }else if (ProjectPictureInfo.getX1()==0){
                        wrapper.eq("ding_user_id",ProjectPictureInfo.getZ()).
                                set("reject_item_score2",ProjectPictureInfo.getY());
                        diyProfessorScoreService.getBaseMapper().update(null,wrapper);
                    }
                    else if (ProjectPictureInfo.getX1()==-4){
                        wrapper.eq("ding_user_id",ProjectPictureInfo.getZ()).
                                set("rejection_score2",ProjectPictureInfo.getY());
                        diyProfessorScoreService.getBaseMapper().update(null,wrapper);
                    }
                }
        );
        for (int i = 0; i < diyProfessorScores2.size(); i++) {
            int a= 0;
            int b= 0;
            int count = 1;
            UpdateWrapper<DiyProjectProfessor> wrapper1 = new UpdateWrapper<>();
            wrapper1.apply("date_format( diy_project_professor.create_time, '%Y') between '2020' and '2021'").eq("ding_user_id",diyProfessorScores2.get(i).getDingUserId());
            List<DiyProjectProfessor> diyProjectProfessors = iDiyProjectProfessorService.getBaseMapper().selectList(wrapper1);
            for (int i1 = 0; i1 < diyProjectProfessors.size(); i1++) {
                a= diyProjectProfessors.get(i1).getProfessorInProjectProfessorStar()+a;
                b=diyProjectProfessors.get(i1).getUgentScore()+b;
            }
            UpdateWrapper<DiyProfessorScore> wrapper2 = new UpdateWrapper<>();
            wrapper2.eq("ding_user_id",diyProfessorScores2.get(i).getDingUserId()).set("item_rating2",a/count).set("ugent_score2",b);
            count++;
            diyProfessorScoreService.update(null,wrapper2);
        }

        //项目分数计算
        List<DiyProfessorScore> diyProfessorScores = diyProfessorScoreService.getBaseMapper().selectList(null);
        diyProfessorScores.forEach(diyProfessorScore1 -> {
            int a=diyProfessorScore1.getEvaluationNumber()-diyProfessorScore1.getRejectionScore()*2-diyProfessorScore1.getRejectItemScore();
            int b=diyProfessorScore1.getEvaluationNumber2()-diyProfessorScore1.getRejectionScore()*2-diyProfessorScore1.getRejectItemScore2();
            int c =diyProfessorScore1.getItemRating()*2+diyProfessorScore1.getItemRating2();
            if (a<0){
                a=0;
            }
            if (b<0){
                b=0;
            }
            UpdateWrapper<DiyProfessorScore> wrapper = new UpdateWrapper<>();
            wrapper.set("item_score",(a*2+b)*100+c*20).eq("ding_user_id",diyProfessorScore1.getDingUserId());
            diyProfessorScoreService.getBaseMapper().update(null,wrapper);
        });

        //获取职称分数

        diyProfessorScores.forEach(diyProfessorScore1 -> {
            List<DiyProfessiorPoint> diyProfessiorPoints = diyProfessiorPointService.getBaseMapper().selectList(null);
            UpdateWrapper<DiyProfessorScore> wrapper = new UpdateWrapper<>();
            if (diyProfessorScore1.getProfessorTitle().equals("国家级专家")){
                wrapper.set("technical_title_score",Integer.parseInt(diyProfessiorPoints.get(0).getPointA())).eq("ding_user_id",diyProfessorScore1.getDingUserId());
                diyProfessorScoreService.getBaseMapper().update(null,wrapper);
            }else
            if (diyProfessorScore1.getProfessorTitle().equals("省级专家")){
                wrapper.set("technical_title_score",Integer.parseInt(diyProfessiorPoints.get(0).getPointB())).eq("ding_user_id",diyProfessorScore1.getDingUserId());
                diyProfessorScoreService.getBaseMapper().update(null,wrapper);
            }else
            if (diyProfessorScore1.getProfessorTitle().equals("正高")){
                wrapper.set("technical_title_score",Integer.parseInt(diyProfessiorPoints.get(0).getPointC())).eq("ding_user_id",diyProfessorScore1.getDingUserId());
                diyProfessorScoreService.getBaseMapper().update(null,wrapper);
            }else
            if (diyProfessorScore1.getProfessorTitle().equals("副高")){
                wrapper.set("technical_title_score",Integer.parseInt(diyProfessiorPoints.get(0).getPointD())).eq("ding_user_id",diyProfessorScore1.getDingUserId());
                diyProfessorScoreService.getBaseMapper().update(null,wrapper);
            }
            if (diyProfessorScore1.getProfessorTitle().equals("中级")){
                wrapper.set("technical_title_score",Integer.parseInt(diyProfessiorPoints.get(0).getPointE())).eq("ding_user_id",diyProfessorScore1.getDingUserId());
                diyProfessorScoreService.getBaseMapper().update(null,wrapper);
            }else
            if (diyProfessorScore1.getProfessorTitle().equals("初级")){
                wrapper.set("technical_title_score",Integer.parseInt(diyProfessiorPoints.get(0).getPointF())).eq("ding_user_id",diyProfessorScore1.getDingUserId());
                diyProfessorScoreService.getBaseMapper().update(null,wrapper);
            }
        });
        //总分
        diyProfessorScores.forEach(diyProfessorScore1 -> {
            Double firstScore = (double)diyProfessorScore1.getFirstScore();
            Double technicalTitleScore =(double) diyProfessorScore1.getTechnicalTitleScore();
            Double itemScore = (double)diyProfessorScore1.getItemScore();
            System.out.println(firstScore);
            Double a =firstScore*0.3+technicalTitleScore*0.2+itemScore*0.5;
            if (a<0){
                a=0.0;
            }
            System.out.println(a);
            UpdateWrapper<DiyProfessorScore> wrapper = new UpdateWrapper<>();
            wrapper.set("abc_score",a).eq("ding_user_id",diyProfessorScore1.getDingUserId());
            diyProfessorScoreService.getBaseMapper().update(null,wrapper);
        });
    }
}
